package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.demos.entity.param.MomentCommentQueryParam;
import com.springboot.cloud.demos.entity.po.MomentComment;
import com.springboot.cloud.demos.producer.dao.MomentCommentMapper;
import com.springboot.cloud.demos.producer.service.IMomentCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
@RefreshScope
public class MomentCommentServiceImpl extends ServiceImpl<MomentCommentMapper, MomentComment> implements IMomentCommentService {

    @Value("${producer.momentComment:123}")
    private String value;

    @Override
    @CacheEvict(value = "momentComment", allEntries = true)
    public boolean add(MomentComment momentComment) {
        return this.save(momentComment);
    }


    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    public boolean update(MomentComment momentComment) {
        return this.updateById(momentComment);
    }

    @Override
    public MomentComment get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    public IPage<Map<String, Object>> query(String momentId, Integer pageNo, Integer pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (momentId != null) {
            queryWrapper.eq("a.moment_id", momentId);
        }
        queryWrapper.orderByDesc("a.created_time");

        Page page = new Page<>(pageNo, pageSize);
        IPage<Map<String, Object>> pageResult = getBaseMapper().selectMapsPage(page, queryWrapper);
        return pageResult;
    }

    @Override
    public Integer queryCount(String momentId) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (momentId != null) {
            queryWrapper.eq("a.moment_id", momentId);
        }

        Integer count = getBaseMapper().selectCount(queryWrapper);
        return count;
    }

}
